package com.bawei.a2010douyin.base.mvp;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;

import com.bawei.a2010douyin.event.Event;
import com.jaeger.library.StatusBarUtil;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

//如果activity有网络mvp请求继承BaseMvpActivity
public abstract class BaseMvpActivity<V,P extends BasePresenter<V>> extends AppCompatActivity implements BaseView {
    protected P mPresenter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(bindLayout());
        //沉浸式状态栏
        StatusBarUtil.setTransparentForImageView(this,null);
        initView(savedInstanceState);
        mPresenter = createPresenter();
        initData();
        //绑定
        if(mPresenter != null){
            mPresenter.attachView((V) this);
        }
        //注册广播
        EventBus.getDefault().register(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if(mPresenter != null){
            mPresenter.distach();
        }
        EventBus.getDefault().unregister(this);
    }
    //接受方法
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void receive(Event event){
        //判断是否为自己发送的广播
        if (event.target.equals(getClass().getSimpleName())){
            onEvent(event);
        }
    }
    //子类重写该方法就可以处理接受数据，不要在base里面做具体的处理
    public void onEvent(Event event){}

    public abstract int bindLayout();
    public abstract void initView(Bundle savedInstanceState);
    public abstract P createPresenter();//实例化presenter
    public abstract void initData();





    @Override
    public void onOk() {

    }

    @Override
    public void onError() {

    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }
}